package com.smforj.ssm.mina.service;

import java.io.File;
import java.io.IOException;

import javax.imageio.stream.FileImageOutputStream;

import org.apache.log4j.Logger;

import com.smforj.ssm.mina.client.IMinaClent;
import com.smforj.ssm.mina.client.MinaClient;
import com.smforj.ssm.mina.message.MessageModel;

@SuppressWarnings("unused")
public class TsFaceService implements Runnable {
	private MessageModel model;
	public TsFaceService(MessageModel model){ this.model = model;}
	private static Logger log = Logger.getRootLogger(); 
	
	private void toPrint(Object model,String ip)
	{
		if(model==null) return;
		System.out.println("发送到 IP："+ip+" 开始打印...");
		IMinaClent client = new MinaClient();  
		client.setHost(ip);
        if (client.iconnect()) { 
        	//System.out.println("打印内容:" + model.toPrint());
            //client.send(model.toPrint(true)); 
            client.close();
        }
	}
	
	public Boolean doMessage() { 
		System.out.println("保存信息doMessage"); 
		return true;
	}
	
	/**
	 * 保存图片到本地
	 * @param img
	 * @return
	 */
	private String SaveImgToLocation(byte[] img, String imgname)
	{ 
		if(img != null && img.length > 0)
		{
			//String newFilename= System.getProperty("user.dir")+ File.separator + "images"
			//		+ File.separator + idcard + ".jpg";//String.format("%s.jpg", idcard);
			//重新命名图片名称
			//String simgName = String.format("%s_%s.jpg", imgname,System.currentTimeMillis());
			
			String newFilename=  System.getProperty("user.dir") + File.separator + "WebRoot/static/tsimage" + File.separator + imgname + ".jpg";
			//"F:/workspace/tsms/WebRoot/static/tsimage" + File.separator + imgname + ".jpg";
	       System.out.println("save faceid images: " + newFilename);
	        try {
	        	FileImageOutputStream imgout=new FileImageOutputStream(new File(newFilename));
				imgout.write(img,0,img.length);
				imgout.close();
			} catch (IOException e) { 
				e.printStackTrace();
			} 
	        return imgname + ".jpg";
		}else
			return "";
	}

	
	@Override
	public void run() {
		//未成功时尝试的次数
		Integer iTryCount = 3;
		while(iTryCount-- > 0)
		{
			if(doMessage())
				break;
		}
		
	} 

}
